﻿using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using Services.IServices.IMinioFileService;

namespace Base.Web.Api.Controllers.File
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileController : BaseController
    {
        private readonly IMinioFileService _fileService;

        public FileController(IMinioFileService fileService)
        {
            _fileService = fileService;
        }

        [HttpPost("Deleting")]
        public async Task<IActionResult> DeleteFile([FromBody] FileDownloadReq request)
        {
            if (await _fileService.DownloadAsync(request.FileGuid))
            {
                return Ok("File deleted successfully.");
            }
            return NotFound("File not found.");
        }
        [HttpGet("GetMinioInfo")]
        public async Task<IActionResult> GetMinioInfo()
        {
            try
            {
                var result = ApiResult.Success("OK");
                return Ok(result);
            }
            catch (Exception ex)
            {
                // Log the exception details (replace with proper logging in production)
                Console.WriteLine($"Exception: {ex.Message}\nStackTrace: {ex.StackTrace}");
                return StatusCode(500, "An unexpected error occurred.");
            }
        }


        [HttpPost("GetFileInfo")]
        public async Task<IActionResult> GetFileInfo([FromBody] FileDownloadReq request)
        {
            try
            {
                bool exists = await _fileService.FileExistsAsync("your-bucket-name", request.FileGuid);
                if (exists)
                {
                    // Assuming you return a basic info object, replace with actual details as needed
                    var fileInfo = new
                    {
                        FileName = request.FileGuid, // or some other way to get the file name
                        Size = "unknown" // Placeholder; MinIO client doesn't provide file size directly in this example
                    };
                    return Ok(fileInfo);
                }
                return NotFound("File not found.");
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }

        [HttpPost("Downloading")]
        public async Task<IActionResult> DownloadFile([FromBody] FileDownloadReq request)
        {
            try
            {
                var stream = await _fileService.DownloadFileAsync("your-bucket-name", request.FileGuid);
                if (stream != null)
                {
                    return File(stream, "application/octet-stream", request.FileGuid); // Adjust content type as needed
                }
                return NotFound("File not found.");
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }

        [HttpPost("GetInfos")]
        public async Task<IActionResult> GetFilesInfo([FromBody] List<FileDownloadReq> requests)
        {
            var results = new List<object>();

            foreach (var request in requests)
            {
                try
                {
                    bool exists = await _fileService.FileExistsAsync("your-bucket-name", request.FileGuid);
                    if (exists)
                    {
                        var fileInfo = new
                        {
                            FileName = request.FileGuid,
                            Size = "unknown" // Placeholder; MinIO client doesn't provide file size directly in this example
                        };
                        results.Add(fileInfo);
                    }
                    else
                    {
                        results.Add(new { FileName = request.FileGuid, Status = "Not Found" });
                    }
                }
                catch (Exception ex)
                {
                    results.Add(new { FileName = request.FileGuid, Status = $"Error: {ex.Message}" });
                }
            }

            return Ok(results);
        }
    }
}
